
import { cookies } from "next/headers"
import { Shell } from "@/components/shell"
import DocumentContainer from "@/components/documents/document-container"
import { getVendorProjectsAndContracts } from "@/lib/vendor-data/services"
import { getVendorDocumentLists } from "@/lib/vendor-document/service"
import VendorDocumentsClient from "@/components/documents/vendor-docs.client"
import VendorDocumentListClient from "@/components/document-lists/vendor-doc-list-client"
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { getServerSession } from "next-auth";



// Layout 컴포넌트는 서버 컴포넌트입니다
export default async function VendorDocuments({
  children,
}: {
  children: React.ReactNode
}) {
  const session = await getServerSession(authOptions)
  const vendorId = session?.user.companyId
  // const vendorId = "17"
  const idAsNumber = Number(vendorId)

  const projects = await getVendorProjectsAndContracts(idAsNumber);
  const filteredProjects = projects.filter(v=>v.projectType === "plant")


  // 레이아웃 설정 쿠키 가져오기
  // Next.js 15에서는 cookies()가 Promise를 반환하므로 await 사용
  const cookieStore = await cookies()

  // 이제 cookieStore.get() 메서드 사용 가능
  const layout = cookieStore.get("react-resizable-panels:layout:mail")
  const collapsed = cookieStore.get("react-resizable-panels:collapsed")

  const defaultLayout = layout ? JSON.parse(layout.value) : undefined
  const defaultCollapsed = collapsed ? JSON.parse(collapsed.value) : undefined


  return (
    <Shell className="gap-2">
      <VendorDocumentListClient projects={filteredProjects}>
        {children}
      </VendorDocumentListClient>
    </Shell>
  )
}